#include "myserver.h"
#include <QHostAddress>
#include <QTcpSocket>
#include <QThread>
#include "worker.h"
#include <QByteArray>
#include <QDebug>
#include <QString>

MyServer::MyServer(QObject *parent) : QObject(parent)
{
    qDebug() << "Server Thread id: " << QThread::currentThread();
    server = new QTcpServer(this);

    server->listen(QHostAddress::Any, quint16(8888));
    qDebug() << "waitting fo connection ...";
    connect(server, SIGNAL(newConnection()),
            this, SLOT(newConnection()));
}

MyServer::~MyServer()
{

}

void MyServer::newConnection()
{
    client = server->nextPendingConnection();
    // 显示连接的信息
    qDebug() << "new conntion form: "
             << client->peerAddress().toString()
             << "-" << client->peerPort();
    //
    Worker *worker = new Worker(client);
    QThread *workthread = new QThread(this);
    worker->moveToThread(workthread);
    connect(client, SIGNAL(readyRead()), worker,SLOT(doWork()));
    connect(client, SIGNAL(disconnected()), workthread, SLOT(quit()));

    workthread->start();
}



